package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class MatiereColorChooser extends JPanel implements ChangeListener {

	private static JDialog colorDialog;
	private JColorChooser tcc;
	private JButton valider;
	private Color newColor;
	private SeanceUI seance;
	
	public MatiereColorChooser() {
        super(new BorderLayout());
        
        tcc = new JColorChooser();
        tcc.getSelectionModel().addChangeListener(this);
        tcc.setBorder(BorderFactory.createTitledBorder(
                                             "Choose Color"));

        add(tcc, BorderLayout.CENTER);
        
        valider = new JButton("Valider");
        add(valider, BorderLayout.PAGE_END);
        
        valider.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == valider) {
					colorDialog.dispose();
				}
				
			}
        	
        });
        
        tcc.setPreviewPanel(makePreviewPanel(tcc.getColor()));
    }

	public void stateChanged(ChangeEvent e) {
		newColor = tcc.getColor();
		seance.setColor(newColor);
		//tcc.repaint();
	}

	private JPanel makePreviewPanel(Color color) {
		JPanel previewPanel = new JPanel();
		previewPanel.setPreferredSize(new Dimension(200,70));
		previewPanel.setLayout(new GridLayout(1,0));
		previewPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 0, Color.BLACK));
		seance = new SeanceUI(null, 0, color, "Algo", "CM", "Ferrié", "IG", "4", "102");
		previewPanel.add(seance);
		
		return previewPanel;
	}
	
	public static Color showColorChooser(JFrame frame) {
		MatiereColorChooser colorChooser = new MatiereColorChooser();
		colorDialog = new JDialog(frame, "Choix de la couleur", true);
		
		colorDialog.add(colorChooser);
		colorDialog.pack();
		colorDialog.setVisible(true);

		return colorChooser.newColor;
	}
}
